﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.IService.JJFlow.Approvers;
using JinJia.Core.Base.Exceptions;

namespace Jinjia.Approve.Service.JJFlow.Approvers
{
    public class Dingding2ndLevelApprovers : JJApproversCommon, IJJApprovers
    {
        public Dingding2ndLevelApprovers(IJJApproversRepository jjApproversRepository) : base(jjApproversRepository)
        {
        }

        public async Task<List<int>> GetInstanceNodeEmpInfo(string biz_order_id, int start_user = 0, string depart_id = "0", Dictionary<string, string> dic = null)
        {
            var approvers = new List<int>();
            //1获取发起人所在完整组织架构
            var full_org = await GetOrgStructure(start_user);
            try
            {
                //2找到发起部门
                foreach (var upper_dept in full_org.upperDept)
                {
                    if (depart_id == upper_dept.ding_user_id) //钉钉的部门id
                    {
                        //3获取此部门的领导
                        if (start_user != upper_dept.leaderNode.user_id) //如果此人不是部门领导
                        {
                            approvers.Add(upper_dept.upperDept[0].leaderNode.user_id);
                            return approvers;
                        }
                        else
                        {
                            //此人是部门领导，则需要再向上找一级
                            approvers.Add(upper_dept.upperDept[0].upperDept[0].leaderNode.user_id);
                            return approvers;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw new BusinessException("未找到审批人，或此人无上级");
            }

            throw new BusinessException("未找到审批人，或此人无上级");
        }
    }
}